package cn.wang.service.cargo.Impl;

import cn.wang.dao.BaseDao;
import cn.wang.entity.cargo.Contract;
import cn.wang.entity.cargo.ExtCproduct;
import cn.wang.entity.cargo.ExtCproduct;
import cn.wang.service.cargo.ExtCproductService;
import cn.wang.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by john on 2017/11/1.
 */
@Service("extCproductProductServiceImpl")
@Transactional
class ExtCproducterviceImpl implements ExtCproductService {
    @Autowired
    private BaseDao<ExtCproduct,String>cpDao;
    @Autowired BaseDao<ExtCproduct,String>extCproductBaseDao;
    @Autowired
    private BaseDao<Contract,String>contractBaseDao;
    @Override
    public Pagination findPage(int pageNo, int pageSize) {


        return cpDao.pageByHql("from ExtCproduct",pageNo,pageSize);
    }

    @Override
    public List<ExtCproduct> findAll() {
        String ctype="货物";
        return extCproductBaseDao.getListByHQL("from ExtCproduct where ctype=? ",ctype);
    }

    @Override
    public void save(ExtCproduct extCproduct) {
        //设置总金额
        double subCunber=extCproduct.getPrice()*extCproduct.getCnumber();
        extCproduct.setAmount(subCunber);
       Contract contract=contractBaseDao.get(Contract.class,extCproduct.getContractProduct().getContract().getId());
        contract.setTotalAmount(contract.getTotalAmount()+subCunber);
       //保存合同总金额
        contractBaseDao.update(contract);
        cpDao.save(extCproduct);
    }



    @Override
    public void update(ExtCproduct model) {
    //TODO 不能为负数
      //设置货物金额 数量*单价
        Double updatesubCunber=0.0;
        double subCunber=model.getPrice()*model.getCnumber();
        model.setAmount(subCunber);
        //查询出老数据做对比
        ExtCproduct extCproduct=extCproductBaseDao.get(ExtCproduct.class,model.getId());
        Double  oldsubCunber=extCproduct.getAmount();
        Contract contract=contractBaseDao.get(Contract.class,model.getContractProduct().getContract().getId());
        if(subCunber>oldsubCunber){
            //更新后金额比以前大 则合同金额加增加的
            updatesubCunber= subCunber-oldsubCunber;
            contract.setTotalAmount(contract.getTotalAmount()+updatesubCunber);

        }else{
            //如果更新后比原来还小 则减去差值
            updatesubCunber=oldsubCunber-subCunber;
            contract.setTotalAmount(contract.getTotalAmount()-updatesubCunber);
        }
        //设置合同总金额 老数据+货物总金额
        //查询出合同金额数据数据
        //保存合同总金额
        cpDao.evcit(extCproduct);
       contractBaseDao.update(contract);
        cpDao.update(model);
    }

    @Override
    public ExtCproduct findById(String id) {
        return cpDao.get(ExtCproduct.class,id);
    }

    /**
     *   删除时设置合同总金额 老数据减去被删产品金额
     * @param ids
     */
    @Override
    public void delete(String[] ids,String id) {
        //业务层放到service 保证在同一事务
        //查询出老金额数据
        Double subContract=0.0;
        for (String cpid:ids) {
            subContract+= cpDao.get(ExtCproduct.class,cpid).getAmount();
            cpDao.deleteById(ExtCproduct.class,cpid);
        }

        Contract contract=contractBaseDao.get(Contract.class,id);
        contract.setTotalAmount(contract.getTotalAmount()-subContract);
        contractBaseDao.update(contract);
    }

    @Override
    public Pagination findPage1(int pageNo, int pageSize, String id) {
        return  contractBaseDao.pageByHql("from ExtCproduct cp where cp.contractProduct.id='"+id+"'",pageNo,pageSize);

    }

    @Override
    public void deleteById(ExtCproduct model) {
        //删之前修改合同金额
        ExtCproduct extCproduct=cpDao.get(ExtCproduct.class,model.getId());
        Double subContract= extCproduct.getAmount();
        Contract contract=contractBaseDao.get(Contract.class,model.getContractProduct().getContract().getId());
        contract.setTotalAmount(contract.getTotalAmount()-subContract);
        contractBaseDao.update(contract);
        cpDao.deleteById(ExtCproduct.class,model.getId());
    }
}
